//
//  MBCommodityRecommendCell.swift
//  iPhone
//
//  Created by Toj on 8/10/22.
//

import UIKit

protocol MBCommodityRecommendCellDelegate: NSObjectProtocol {
    
    func commodityRecommendCellDidTapRecommend(_ view: MBCommodityRecommendCell)
}

private let buyButtonHeight: CGFloat = 32

// MARK: - 懒加载(属性), 变量, 初始化init
class MBCommodityRecommendCell: UICollectionViewCell {
    
    weak var delegate: MBCommodityRecommendCellDelegate?
    
    var recommendProduct: MemberProductDto? { didSet {
        guard let product = recommendProduct else { return }
        
        let url = URL(string: product.product_logo)
        iconImageView.kf.setImage(with: url, placeholder: "ph_star_logo".to_image)
        nameLabel.text = product.name
        
        if !product.isLoad {
            product.isLoad = true
            DataStatistics.sendEvent(event: DSEvent(category: st_className, action: "ProdEntry", label: product.name, value: "1", lt: .event))
        }
    }}
    
    @objc private func didTapRecommend() {
        delegate?.commodityRecommendCellDidTapRecommend(self)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        
        let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer.init(target:self, action:#selector(didTapRecommend))
        addGestureRecognizer(tapGesture)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()

        setupMakeLayoutSubviews()
    }
    
    private lazy var iconImageView: UIImageView = UIImageView()
    
    private lazy var arrowsImageView: UIImageView = {
       let view = UIImageView()
        view.image = "commodity_arrows".to_image
        return view
    }()
    
    private lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont.systemFont(ofSize: 16.f_scale)
        view.textColor = "#333333".to_color
        return view
    }()
}

// MARK: - 初始化
extension MBCommodityRecommendCell {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(iconImageView)
        contentView.addSubview(arrowsImageView)
        contentView.addSubview(nameLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        
        iconImageView.snp.makeConstraints { make in
            make.left.centerY.equalTo(contentView)
            make.height.equalTo(32)
            make.width.equalTo(32)
        }
        
        arrowsImageView.snp.makeConstraints { make in
            make.right.centerY.equalTo(contentView)
            make.height.equalTo(24)
            make.width.equalTo(24)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(contentView)
            make.left.equalTo(iconImageView.snp.right).offset(8)
            make.right.equalTo(arrowsImageView.snp.left).offset(-8)
        }
    }
}
